package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @GetMapping("/getMenuListByUsername")
    public Result getMenuByUserId(String username){
        try {
            List<Map<String,Object>> menuList = menuService.getMenuListByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }
    }
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
                    PageResult pageResult = menuService.pageQuery(queryPageBean);
                    return new Result(true,"查询菜单列表成功",pageResult);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单列表失败");
        }
    }
    @PostMapping(path = "/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增菜单失败");
        }
    }
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            //查询检查项
            Menu menu = menuService.findById(id);
            return new Result(true, "查询表单数据成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询表单数据失败");
        }
    }
    @PutMapping(path = "/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true,"修改菜单信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改菜单信息失败");
        }
    }
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            //删除检查项
            menuService.deleteById(id);
            return new Result(true,"删除菜单成功");
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败");
        }
    }

    //查询菜单列表
    @RequestMapping("/findAll")
    public  Result findAll(){
   try {
      List<Menu> list=menuService.findAll();
      return new Result(true,"查询菜单信息成功",list);
   }catch (Exception e){
       return  new Result(false,"查询菜单信息失败");
   }
    }

}
